# -*- coding: utf-8 -*-
"""
# --------------------------------------------------------
# @Project: face-landmark-detection
# @Author : Pan
# @E-mail :
# @Date   : 2020-07-31 10:28:14
# --------------------------------------------------------
"""
# -*-coding: utf-8 -*-
import sys
import os

sys.path.append(os.getcwd())
import base64
import cv2
import numpy as np
import PIL.Image as Image
from src import demo

det = demo.FaceLandmarkDetection()


def get_face_landmark_from_image(image):
    return det.detect_face_landmark(image, isshow=False)


def get_face_landmark_from_base64(image_base64):
    header = 'data:image/jpeg;base64,'
    assert header == image_base64[:len(header)]
    image_base64 = image_base64[len(header):]
    image_base64 = bytes(image_base64, 'utf-8')
    image = base64.b64decode(image_base64)
    image = np.fromstring(image, np.uint8)
    image = cv2.imdecode(image, cv2.IMREAD_COLOR)
    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    return get_face_landmark_from_image(image)


if __name__ == "__main__":
    image_path = "./src/data/test1.jpg"
    # image = cv2.imread(image_path)
    # image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    # result = get_face_landmark_from_image(image)
    image_base64 = 'data:image/jpeg;base64,' + base64.b64encode(open(image_path, 'rb').read()).decode()
    result = get_face_landmark_from_base64(image_base64)
    print(result)
